package app;

import java.util.*;

import com.StarMicronics.StarIOSDK.R;

import utils.FilePack;
import utils.IntConvert;

import android.content.Context;
import android.graphics.*;
import android.graphics.drawable.Drawable;
import android.os.*;
import android.view.*;
import android.view.View.*;
import android.widget.*;

public class AcceptTerms {
	private EditText terms;
	private PaintingView signature;
	private ImageView accept;
	private EditText name;
	private View magicView;
	private ImageView lastSign;
	
	private long ticketID;
	private boolean bAccept;
	private Vector<Bitmap> signatures;
	
	public AcceptTerms(long param) {
		signatures = new Vector<Bitmap>();
		loadXml();
		
		ticketID = param;
		//[signature setBrushColorWithRed:0 green:0 blue:0]
		terms.setText(AppDelegate.sharedInstance().getMain().term);
		Dictionary<String, Object> dic = AppDelegate.sharedInstance().getMain().ticketsDataSource.memoryObject.get(Long.valueOf(ticketID));
		if (dic != null) {
			bAccept = IntConvert.parseInt(dic.get(TicketList.ACCEPT_TERMS)) != 0;
			if (bAccept) {
				accept.setImageDrawable(view.getResources().getDrawable(R.drawable.check));
			} else {
				accept.setImageDrawable(view.getResources().getDrawable(R.drawable.uncheck));
			}
			if (dic.get(TicketList.SIGNATURE) != null) signatures.addAll((Vector<Bitmap>)dic.get(TicketList.SIGNATURE));
			
			if (signatures.size() > 0) {
				lastSign.setImageBitmap(signatures.lastElement());
				lastSign.setVisibility(View.VISIBLE);
			}
			
			name.setText((String)dic.get(TicketList.SIGN_NAME));
		}
		
		AppDelegate.sharedInstance().layout.addView(view);
	}
	
	private void clear() {
		signature.erase();
	}
	
	private void addSignature() {
		Bitmap temp = signature.captureView();
		if (lastSign.getVisibility() == View.VISIBLE) {
			lastSign.setVisibility(View.INVISIBLE);
		} else if (temp != null) {
			signatures.add(temp);
		} else {
			WaittingAlertView alert = WaittingAlertView.createWaittingAlertView();
			alert.setTitle(FilePack.getStringByID(STRING_ID.STR_SIGNATURE_TOO_SIMPLE));
			alert.show();
		}
		signature.erase();
	}
	
	public void back() {
		AppDelegate.sharedInstance().layout.removeView(view);
	}
	
	private void received() {
		Bitmap temp = signature.captureView();
		if (temp != null) {
			signatures.add(temp);
		}
		
		if (signatures.size() == 0) {
			WaittingAlertView alert = WaittingAlertView.createWaittingAlertView();
			alert.setTitle(FilePack.getStringByID(STRING_ID.STR_SIGNATURE_TOO_SIMPLE));
			alert.show();
		} else if (name.getText().toString().length() == 0) {
			WaittingAlertView alert = WaittingAlertView.createWaittingAlertView();
			alert.setTitle(FilePack.getStringByID(STRING_ID.STR_WITHOUT_NAME));
			alert.show();
		} else if (bAccept == false) {
			WaittingAlertView alert = WaittingAlertView.createWaittingAlertView();
			alert.setTitle(FilePack.getStringByID(STRING_ID.STR_WITHOUT_ACCEPT_TERMS));
			alert.show();
		} else {
			Dictionary<Long, Dictionary<String, Object>> a = AppDelegate.sharedInstance().getMain().ticketsDataSource.memoryObject;
			Dictionary<String, Object> dic = a.get(Long.valueOf(ticketID));
			if (dic != null) {
				dic.put(TicketList.ACCEPT_TERMS, bAccept?"1":"0");
				dic.put(TicketList.SIGN_NAME, name.getText().toString());
				dic.put(TicketList.SIGNATURE, signatures);
				
				Dictionary<String, Object>param = new Hashtable<String, Object>();
				Vector<Long> changed = new Vector<Long>();
				changed.add(Long.valueOf(ticketID));
				param.put("changed", changed);
				AppDelegate.sharedInstance().postNotification(DataSourceChangeNotificationListener.class, AppDelegate.sharedInstance().getMain().ticketsDataSource, param);
			}
			back();
		}
	}
	
	private void acceptChanged() {
		bAccept ^= true;
		if (bAccept) {
			accept.setImageDrawable(view.getResources().getDrawable(R.drawable.check));
		} else {
			accept.setImageDrawable(view.getResources().getDrawable(R.drawable.uncheck));
		}
	}
	
	private void reject() {
		RejectView reject = new RejectView(ticketID);
		reject.accceptView = this;
	}
	
	private void resign() {
		
	}
	
	
	
	private View view;
	private void loadXml() {
		TextView text;
		Button button;
        view = AppDelegate.sharedInstance().activity.getLayoutInflater().inflate(R.layout.accept_terms, null);
        magicView = view.findViewById(R.id.magic_view);
        
        text = (TextView) view.findViewById(R.id.term_title);
        text.setText(FilePack.getStringByID(STRING_ID.STR_TERM_TITLE));
        
        terms = (EditText) view.findViewById(R.id.terms);
        
        text = (TextView) view.findViewById(R.id.sign_hint);
        text.setText(FilePack.getStringByID(STRING_ID.STR_SIGN_HINT));
        
        accept = (ImageView) view.findViewById(R.id.accept);
        
        text = (TextView) view.findViewById(R.id.accept_read);
        text.setText(FilePack.getStringByID(STRING_ID.STR_ACCEPT_READ));

        signature = (PaintingView) view.findViewById(R.id.signature);
        lastSign = (ImageView) view.findViewById(R.id.lastSign);
        
        View v = view.findViewById(R.id.accept_changed);
        v.setOnClickListener(new OnClickListener() {
			public void onClick(View arg0) {
				acceptChanged();
			}
		});
        
        name = (EditText) view.findViewById(R.id.name);
        name.setHint(FilePack.getStringByID(STRING_ID.STR_NAME));
        
        button = (Button)view.findViewById(R.id.button_clear);
        button.setText(FilePack.getStringByID(STRING_ID.STR_CLEAR));
        button.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				clear();
			}
		});
        
        button = (Button)view.findViewById(R.id.button_add_signature);
        button.setText(FilePack.getStringByID(STRING_ID.STR_ADD_SIGNATURE));
        button.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				addSignature();
			}
		});

        button = (Button)view.findViewById(R.id.button_reject);
        button.setText(FilePack.getStringByID(STRING_ID.STR_REJECT));
        button.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				reject();
			}
		});

        button = (Button)view.findViewById(R.id.button_received);
        button.setText(FilePack.getStringByID(STRING_ID.STR_ACCEPT));
        button.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				received();
			}
		});

        button = (Button)view.findViewById(R.id.button_back);
        button.setText(FilePack.getStringByID(STRING_ID.STR_BACK));
        button.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				back();
			}
		});

        view.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				resign();
			}
		});
	}
	
}
